package com.zhangxu.basemvp.base;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.zhangxu.basemvp.utils.ToastUtil;

/**
 * Time:2019/4/10
 * Author:zhangxu
 */
public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements BaseView {
    protected P mPresenter;
    private ProgressDialog progDialog;
    @Override
    public void showMessage(String msg) {
        ToastUtil.showLongToast(this, msg);
    }

    @Override
    public void showLoading() {
        if (progDialog == null) {
            progDialog = new ProgressDialog(this);
            progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progDialog.setIndeterminate(false);
            progDialog.setCancelable(true);
            progDialog.setMessage("正在加载");
        }
        progDialog.show();
    }

    @Override
    public void dismissLoading() {
        if (progDialog != null) {
            progDialog.dismiss();
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //创建Presenter
        mPresenter = createPresenter();
        //关联View
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
        BaseApplication.getInstance().addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mPresenter!=null){
            mPresenter.detachView();
        }
        BaseApplication.getInstance().finishActivity(this);
    }

    protected abstract P createPresenter();

}
